#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 - 2013 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1

#include <Macros.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Event.h>
#pragma unmanaged 
#include <ql\cashflow.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;
using namespace Cephei::Core;
using namespace PLATFORM::Collections;

#define HANDLE
#define ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICashFlow
	public ref class CCashFlow  : 
            public CEvent,
            public Cephei::QL::ICashFlow
	{
	protected: 
		boost::shared_ptr<QuantLib::CashFlow>* _ppCashFlow;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CashFlow>* _phCashFlow;
#endif
		Object^ _CashFlowOwner;     // reference to object that manages the storage for this object
	internal:
        CCashFlow (boost::shared_ptr<QuantLib::CashFlow>& childNative, Object^ owner);
        CCashFlow (QuantLib::CashFlow& childNative, Object^ owner);
        CCashFlow (CCashFlow^ copy);
        CCashFlow (PLATFORM::Type^ t);
#ifdef STRUCT
        CCashFlow (QuantLib::CashFlow childNative);
#endif       
#ifdef HANDLE
		CCashFlow (QuantLib::Handle<QuantLib::CashFlow>& childNative, Object^ owner);
		CCashFlow (QuantLib::Handle<QuantLib::CashFlow> childNative);
#endif
		virtual ~CCashFlow ();
		!CCashFlow ();

	internal:
		QuantLib::CashFlow& GetReference ();
		boost::shared_ptr<QuantLib::CashFlow>& GetShared ();
		QuantLib::CashFlow* GetPointer ();
        void SetCashFlow (boost::shared_ptr<QuantLib::CashFlow> native)
        {
            if (_ppCashFlow != NULL)
                delete _ppCashFlow;
            _ppCashFlow = new boost::shared_ptr<QuantLib::CashFlow> (native);
            SetEvent (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCashFlow));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CashFlow>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
		virtual Boolean HasOccurred (Microsoft::FSharp::Core::FSharpOption<DateTime>^ refDate, Microsoft::FSharp::Core::FSharpOption<Boolean>^ includeRefDate) ;
        property Double Amount 
        {
		    virtual Double get () ;
        }
        property DateTime Date 
        {
		    virtual DateTime get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICashFlow Vector interface
	ref class CCashFlowVector sealed : 
							    public Core::INativeVector<ICashFlow^>,
							    public Core::INativeVector<Core::Generic::ICoCell<ICashFlow^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::CashFlow>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::CashFlow>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::CashFlow>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CCashFlowVector ();
#ifdef STRUCT
		CCashFlowVector (std::vector<QuantLib::CashFlow>& vec, Object^ owner);
		CCashFlowVector (std::vector<QuantLib::CashFlow> vec);
#endif       
		CCashFlowVector (std::vector<boost::shared_ptr<QuantLib::CashFlow>>& vec, Object^ owner);
#ifdef HANDLE
		CCashFlowVector (std::vector<QuantLib::Handle<QuantLib::CashFlow>>& vec, Object^ owner);
#endif
		virtual ~CCashFlowVector ();
		!CCashFlowVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property ICashFlow^ default[int] 
		{
			virtual ICashFlow^ get (int index) = Core::INativeVector<ICashFlow^>::default::get;
			virtual void set (int index, ICashFlow^ value) = Core::INativeVector<ICashFlow^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^ CItem[int] 
		{
			virtual Core::Generic::ICoCell<ICashFlow^>^ get (int index) = Core::INativeVector<Core::Generic::ICoCell<ICashFlow^>^>::default::get;
			virtual void set (int index, Core::Generic::ICoCell<ICashFlow^>^ value) = Core::INativeVector<Core::Generic::ICoCell<ICashFlow^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::CashFlow>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::CashFlow>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::CashFlow>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICashFlow Matrix interface
	ref class CCashFlowMatrix sealed : 
								public Core::INativeMatrix<ICashFlow^>,
							    public Core::INativeMatrix<Core::Generic::ICoCell<ICashFlow^>^>
	{
	private:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::CashFlow>>* _pVValue;
#endif       
		std::vector<std::vector<boost::shared_ptr<QuantLib::CashFlow>>>* _pVBoost;
#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::CashFlow>>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner
	internal:
		CCashFlowMatrix ();
#ifdef STRUCT        
		CCashFlowMatrix (std::vector<std::vector<QuantLib::CashFlow>>& vec, Object^ owner);
		CCashFlowMatrix (std::vector<std::vector<QuantLib::CashFlow>> vec);
#endif        
		CCashFlowMatrix (std::vector<std::vector<boost::shared_ptr<QuantLib::CashFlow>>>& vec, Object^ owner);
#ifdef HANDLE
		CCashFlowMatrix (std::vector<std::vector<QuantLib::Handle<QuantLib::CashFlow>>>& vec, Object^ owner);
#endif
		virtual ~CCashFlowMatrix ();
		!CCashFlowMatrix ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property Cephei::QL::ICashFlow^ default[int, int] 
		{
			virtual ICashFlow^ get (int x, int y) = Core::INativeMatrix<ICashFlow^>::default::get;
			virtual void set (int x, int y, ICashFlow^ value) = Core::INativeMatrix<ICashFlow^>::default::set;
		}

		property Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^ CItem[int, int] 
		{
			virtual Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^ get (int x, int y) = Core::INativeMatrix<Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^>::default::get;
			virtual void set (int x, int y, Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^ value) = Core::INativeMatrix<Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^>::default::set;
		}

    public:
		property Core::INativeVector<Cephei::QL::ICashFlow^>^ default[int] 
		{
			virtual Core::INativeVector<Cephei::QL::ICashFlow^>^ get (int x) = Core::INativeMatrix<Cephei::QL::ICashFlow^>::default::get
			{
				if ((size_t)x > _pVBoost->size())
					throw REFNEW RangeException ();
				return REFNEW CCashFlowVector ((*_pVBoost)[x], this);
			}
		}
	public:
		property Core::INativeVector<Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^>^ CVItem[int] 
		{
			virtual Core::INativeVector<Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^>^ get (int x) = Core::INativeMatrix<Core::Generic::ICoCell<Cephei::QL::ICashFlow^>^>::default::get
			{
				if ((size_t)x > _pVBoost->size())
					throw REFNEW RangeException ();
				return REFNEW CCashFlowVector ((*_pVBoost)[x], this);
			}
		}

	internal:
#ifdef STRUCT
        std::vector<std::vector<QuantLib::CashFlow>>& GetReference ();
#endif        
		std::vector<std::vector<boost::shared_ptr<QuantLib::CashFlow>>>& GetShared ();

#ifdef HANDLE
		std::vector<std::vector<QuantLib::Handle<QuantLib::CashFlow>>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	public:
		virtual Core::INativeVector<ICashFlow^>^ CreateVector () = Core::INativeMatrix<ICashFlow^>::CreateVector;
	public:
		virtual Core::INativeVector<Core::Generic::ICoCell<ICashFlow^>^>^ CreateCellVector () = Core::INativeMatrix<Core::Generic::ICoCell<ICashFlow^>^>::CreateVector;
	};

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
//z	[FactoryFor(Core::Generic::ICoCell<Cephei::QL::ICashFlow^>::typeid)]
	[FactoryFor(Cephei::QL::ICashFlow::typeid)]
	[FactoryFor(Cephei::QL::ICashFlow_Factory::typeid)]
	public ref class CCashFlow_Factory sealed : public ICashFlow_Factory
	{
	public:
        /* <summary> Create a hybrid Matrix of ICashFlow, with event notification of changes </summary> */
        virtual Cephei::Core::IMatrix<ICashFlow^>^ CreateMatrix();
        /* <summary> Create a hybrid Matrix of ICell of ICashFlow, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IMatrix<Core::Generic::ICoCell<ICashFlow^>^>^>^ CreateCellMatrix();
        virtual Cephei::Core::IMatrix<ICashFlow^>^ CreateMatrix(PLATFORM::Collections::Generic::IEnumerable<PLATFORM::Collections::Generic::IEnumerable<ICashFlow^>^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IMatrix<Core::Generic::ICoCell<ICashFlow^>^>^>^ CreateCellMatrix(PLATFORM::Collections::Generic::IEnumerable<PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<ICashFlow^>^>^>^ source);
        /* <summary> Create a hybrid Vector of ICashFlow, with event notification of changes </summary> */
        virtual Cephei::Core::IVector<ICashFlow^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of ICashFlow, with event notification of changes </summary> */
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<ICashFlow^>^>^>^ CreateCellVector();
        virtual Cephei::Core::IVector<ICashFlow^>^ CreateVector(PLATFORM::Collections::Generic::IEnumerable<ICashFlow^>^ source);
        virtual Core::Generic::ICoCell<Cephei::Core::IVector<Core::Generic::ICoCell<ICashFlow^>^>^>^ CreateCellVector(PLATFORM::Collections::Generic::IEnumerable<Core::Generic::ICoCell<ICashFlow^>^>^ source);
		virtual Core::INativeVector<Cephei::QL::ICashFlow^>^ CreateNativeVector ()
		{
			return REFNEW CCashFlowVector ();
		}

        virtual Core::INativeMatrix<Cephei::QL::ICashFlow^>^ CreateNativeMatrix ()
		{
			return REFNEW CCashFlowMatrix ();
		}

        virtual Core::INativeCube<Cephei::QL::ICashFlow^>^ CreateNativeCube ()
		{
			return nullptr;
		}
    };
   
/*Cephei*/ } /*QL */}
